package com.douqu.game.main.test;


import com.douqu.game.core.protobuf.SGCommonProto;
import com.douqu.game.core.protobuf.SGInstanceProto;
import com.douqu.game.core.protobuf.SGMainProto;
import com.douqu.game.core.protobuf.SGSystemProto;
import com.douqu.game.core.util.BufferUtils;
import com.douqu.game.core.util.SendUtils;
import com.douqu.game.core.util.Utils;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import org.apache.log4j.Logger;

/**
* Created by Administrator on 2016/11/9.
*/
public class NettyTCPClientHandler extends ChannelInboundHandlerAdapter {

    Logger logger = Logger.getLogger(this.getClass());

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception
    {
//        cause.printStackTrace();
        logger.info(cause.getLocalizedMessage());
        ctx.close();
    }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        ByteBuf byteBuf = (ByteBuf) msg;
        int code = BufferUtils.readShort(byteBuf);

        logger.info("data length:" + byteBuf.readableBytes());
        logger.info("code:"+code);
        byte[] data = Utils.byteBufToBytes(byteBuf);
        switch (code){
            case SGMainProto.E_MSG_ID.MsgID_System_Login_VALUE:
                logger.info(SGSystemProto.S2C_Login.parseFrom(data).toString());
                passLevel(ctx);
                getInstanceInfo(ctx);
//                rewardInstance(ctx);
                break;
            case SGMainProto.E_MSG_ID.MsgID_System_Regist_VALUE:
                logger.info(SGSystemProto.S2C_Regist.parseFrom(data).toString());
                break;
            case  SGMainProto.E_MSG_ID.MsgID_Instance_GetInstanceInfo_VALUE:
                logger.info(SGInstanceProto.S2C_GetInstanceInfo.parseFrom(data).toString());
                break;
            case  SGMainProto.E_MSG_ID.MsgID_Instance_ReceiveAward_VALUE:
                logger.info(SGInstanceProto.S2C_ReceiveInstanceAward.parseFrom(data).toString());
                break;
            case SGMainProto.E_MSG_ID.MsgID_Instance_PassLevel_VALUE:
                logger.info(SGInstanceProto.S2C_PassLevel.parseFrom(data).toString());
                break;
        }


    }

    private void passLevel(ChannelHandlerContext ctx) {
        byte []  data =  SGInstanceProto.C2S_PassLevel.newBuilder().
                        setMapId(1)
                        .setLevelId(2)
                        .setStarts(4)
                        .build().toByteArray();
        SendUtils.sendMsg(ctx.channel(), SGMainProto.E_MSG_ID.MsgID_Instance_PassLevel_VALUE, data);
    }

    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
        ctx.flush();// 将消息发送队列中的消息写入到SocketChannel中发送给对方。
    }


    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception
    {
        login(ctx);
//        register(ctx);
//        login(ctx);
//        register(ctx);
//
//        SendUtils.sendMsg(ctx.channel(), SGMainProto.E_MSG_ID.MsgID_System_SuperLogin_VALUE, null);
//
//        ctx.close();
    }

    private void login(ChannelHandlerContext ctx) {
        SGSystemProto.C2S_Login.Builder request = SGSystemProto.C2S_Login.newBuilder();
        request.setChannel(SGCommonProto.E_CHANNEL_TYPE.CHANNEL_TYPE_QUICK);
        request.setNormal(true);
        request.setAccount("wangdada");

        SendUtils.sendMsg(ctx.channel(), SGMainProto.E_MSG_ID.MsgID_System_Login_VALUE, request.build().toByteArray());
    }




    private void register(ChannelHandlerContext ctx) {
        SGSystemProto.C2S_Regist.Builder request = SGSystemProto.C2S_Regist.newBuilder();
        request.setChannel(SGCommonProto.E_CHANNEL_TYPE.CHANNEL_TYPE_QUICK);
        request.setAccount("wangdada");
        request.setAvatarUrl("instance.jpg");
        request.setNickName("instance");

        SendUtils.sendMsg(ctx.channel(), SGMainProto.E_MSG_ID.MsgID_System_Regist_VALUE, request.build().toByteArray());
    }

    private void getInstanceInfo(ChannelHandlerContext ctx) {
        SGInstanceProto.C2S_GetInstanceInfo.Builder request = SGInstanceProto.C2S_GetInstanceInfo.newBuilder();
        request.setMapId(0);
        SendUtils.sendMsg(ctx.channel(), SGMainProto.E_MSG_ID.MsgID_Instance_GetInstanceInfo_VALUE, request.build().toByteArray());
    }

    private void rewardInstance(ChannelHandlerContext ctx) {
        SGInstanceProto.C2S_ReceiveInstanceAward.Builder request =  SGInstanceProto.C2S_ReceiveInstanceAward.newBuilder();
        request.setMapId(1);
        request.setRewardId(10);
        SendUtils.sendMsg(ctx.channel(), SGMainProto.E_MSG_ID.MsgID_Instance_ReceiveAward_VALUE, request.build().toByteArray());
    }

    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception
    {
        logger.info("服务器已关闭，我也要关服了！");
        ctx.close();
    }


}